<?php
//$Id: front.php 207 2012-03-14 09:52:26Z tomqin@gmail.com $

/**
 * FrontController 前台管理基类
 *
 * @copyright       Copyright (C) 2010-2012 ARESCMS Inc.
 * @author          TomQin <tomqin@gmail.com>
 * @license         http://www.arescms.cn/license/
 * @package Core
 */

class FrontController {

    public $controller = '';
    public $action = '';
    public $param = '';
    public $user = null;

    function __destruct() {}

    //@overwrite
    function init(){}

    //调用
    function initialize($controller, $action, $param){
        $this->controller = $controller;
        $this->action     = $action;
        $this->param      = $param;
        $this->user       = new User();

        App::getDb();
        $this->init();
    }

    function setVar($key, $var){
        View::setVar($key, $var);
    }

    function render($tpl){
        $var = explode('-', $tpl);
        View::setVar('__tpl_class', $var[0]);
        View::setVar('__tpl_name', $tpl);
        View::addView($tpl);
        View::display();
    }

    function seoTitle($title){
        $seo = $title .'大连外卖网|大连快餐网|大连订餐网|大连网上订餐 - 速订客|速定客|SuDingKe.com - 速订客，预订美好生活！';
        $kwd = preg_replace('/[\|_ -,]/', ',', $title);

        View::setVar('title', $seo);
        View::setVar('keyword', $kwd);
        View::setVar('description', $seo);
    }

    static function run(){
        @list($tmp, $con, $act, $param) = explode('/', $_SERVER['REDIRECT_URL'], 4);
        $con = empty($con) ? 'default' : $con;
        $act = empty($act) ? 'index' : $act;

        $con_file  = ARESCMS_PATH . 'front' . DS . $con . '.php';
        $con_class = "{$con}_controller";
        if(file_exists($con_file)){
            require_once $con_file;

            $control  = new $con_class;
            $control->initialize($con, $act, $param);
            $method   = "on".$control->action;
            if(method_exists($control, $method)){
                call_user_func(array($control, $method));
            }elseif(method_exists($control, '_onDefault')){
                call_user_func(array($control, '_onDefault'));
            }else{
                echo('The action `' .$act. '` is not exists!');
            }
        }else{
            echo('Front controller `' .$con. '`  is not exists!');
        }
        exit('<!-- Runtime: '.runtime(). '-->');
    }
}

class User{
    var $userId;
    var $userName;

    function __construct(){
        $this->userId = 100;
        $this->userName = 'guest';
        $this->isAdmin = true;
    }

    function getId(){
        return $this->userId;
    }

    function getName(){
        return $this->userId;
    }

    function isAdmin(){
        return $this->isAdmin;
    }

    function isLogin(){
        return $this->isLogin;
    }
}
?>